﻿using System;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.Web.Models;

namespace ASPItalia.ModelVirtualCasting.Web.Filters
{
    public class ShowNewsAttribute : ActionFilterAttribute
    {
        public int Number { get; set; }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var model = filterContext.Controller.ViewData.Model as IHasNews;

            if (model == null)
            {
                throw new InvalidOperationException(
                    "Impossibile usare ShowNews se il model non implementa IHasNews");
            }

            INewsRepository newsRepository = ApplicationContext.Current.News;
            model.News.Clear();
            model.News.AddRange(newsRepository.GetLastNewsDTO(this.Number));
        }
    }
}